
package com.ibeeking.found.open.b.rest.controller.v1;

import com.ibeeking.found.common.annotation.IgnoreResult;
import com.ibeeking.found.open.b.rest.service.IWxOpenEventService;
import com.ibeeking.found.open.service.common.vo.UrlVO;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.log.utils.LogUtils;

import me.chanjar.weixin.open.bean.result.WxOpenAuthorizerInfoResult;
import me.chanjar.weixin.open.bean.result.WxOpenQueryAuthResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @ClassName WxOpenEventController
 * @Description 微信主动调用
 * @Author ibeeking
 * @Date 2021-06-10 16:23
 **/

@RestController
@RequestMapping("/v1/open/api/wxOpenEvent")
public class WxOpenEventController {

    @Resource
    private IWxOpenEventService wxOpenEventServiceImpl;

    /**
     * 开放平台授权事件
     *
     * @param data 微信发送过来的加密的xml格式数据，通过在创建第三方平台是填写的授权事件URL关联
     *             除了接受授权事件（成功授权、取消授权以及授权更新）外，在接受ticket及授权后回调URI也会用到该方法
     * @return 根据微信开放平台规定，接收到授权事件后只需要直接返回success
     */
    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "开放平台授权事件")
    @PostMapping(value = "/{appId}/authTicket")
    @IgnoreResult
    public String authTicket(
            @PathVariable(value = "appId") String componentAppId,
            @RequestBody(required = false) String data,
            @RequestParam("timestamp") String timestamp,
            @RequestParam("nonce") String nonce,
            @RequestParam("signature") String signature,
            @RequestParam(name = "encrypt_type", required = false) String encType,
            @RequestParam(name = "msg_signature", required = false) String msgSignature
    ) {
        return wxOpenEventServiceImpl.authTicket(componentAppId, data, timestamp, nonce, signature, encType, msgSignature);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "开放平台事件消息")
    @PostMapping("/{appId}/msgEvent")
    @IgnoreResult
    public Object msgEvent(
            @PathVariable("appId") String componentAppId,
            @RequestParam("openid") String openid,
            @RequestBody(required = false) String data,
            @RequestParam("timestamp") String timestamp,
            @RequestParam("nonce") String nonce,
            @RequestParam("signature") String signature,
            @RequestParam("encrypt_type") String encType,
            @RequestParam("msg_signature") String msgSignature,
            HttpServletResponse httpServletResponse
    ) {

        return wxOpenEventServiceImpl.msgEvent(componentAppId, openid, data, timestamp, nonce, signature, encType, msgSignature, httpServletResponse);
    }

    /**
     * 授权注册页面扫码授权   显示二维码
     *
     * @param request
     * @return
     * @throws Exception
     * @Author ibeeking
     * param appId 微信公众平台的APPID
     */
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "获取微信公共平台授权页面url")
    @PostMapping(value = "/getPreAuthUrl/{type}")
    public UrlVO getPreAuthUrl(HttpServletRequest request, @PathVariable("type") Integer type) throws Exception {
        return wxOpenEventServiceImpl.getPreAuthUrl(request,type);
    }

}

